<?php
// +----------------------------------------------------------------------
// | WoShiMaiJia Projcet 
// +----------------------------------------------------------------------
// | Copyright (c) 2010-2011 http://woshimaijia.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: xinqiyang <xinqiyang@gmail.com>
// +----------------------------------------------------------------------

/**
 * Account Service
 * do all logic here
 * all the logic of the system,it include  3 table's in the db and many keys  in the redis
 * @author xinqiyang
 *
 */
class AccountService extends BaseService
{
	
	/**
	 * sign up proccess logic
	 * get user city info
	 * @param unknown_type $array
	 */
	public static function signUp($array='')
	{
		//set screenname as account
		$array['screenname'] = $array['account']; 
		$result = self::add('account', $array);
		$uresult = self::add('userinfo', array('id'=>$array['id']));
		if($result && $uresult)
		{
			//add to event queue then deal it
			self::addToEventQueue($array,__CLASS__.':'.__METHOD__);
			return true;
		}else{
			logFatal('account signup error.'.json_encode($array).'  add account'.$result.'   add uresult'.$uresult);
		}
		logDebug('account signup error.'.json_encode($array).'  add account'.$result.'   add uresult'.$uresult);
		return false;
	}
	
	/**
	 * set user session info 
	 * 
	 * @param array $array
	 */
	public static function setSession($id)
	{
		if($id>0){
			$array = self::account(array('id'=>$id));
			$image_id = isset($array['image_id']) ? $array['image_id'] : 0;
			$screenName = isset($array['screenname']) ? $array['screenname'] : $array['account'];
			$uinfo = array('uid'=>$array['id'],'account'=>$array['account'],'screenname'=>$screenName,'image_id'=>$image_id);
		}else{
			$uinfo = array('uid'=>0,'screenName'=>'','icon'=>C('site.defaultIcon'),'image_id'=>0);
		}
		$uinfo['ip'] = getip();
		$uinfo['sessionid'] = session_id();
		//@todo need changeip
		//$uinfo['city'] = Iplocation::city('222.172.200.68');
		Session::set('uinfo', json_encode($uinfo));
		Session::set('uid', $id);
		return ;
	}
	
	/**
	 * set default user info
	 */
	public static function defaultUser(){
		$uinfo = array('uid'=>0,'screenname'=>'','icon'=>C('site.defaultIcon'),'image_id'=>0);
		$uinfo['ip'] = getip();
		$uinfo['sessionid'] = session_id();
		//@todo set city info of the user
		//$uinfo['city'] = Iplocation::city('222.172.200.68');
		self::addToEventQueue($uinfo,__CLASS__.':'.__METHOD__);
		return $uinfo;
	}
	
	/**
	 * save user screenname
	 * @param unknown_type $array
	 */
	public static function saveAccount($array)
	{
		if(isset($array['id']) && isset($array['screenname'])) 
		{
			return self::save('account', $array);
		}
		return false;
	}
	
	/**
	 * set user cookie
	 * account,password
	 * @param unknown_type $array
	 */
	public static function setCookie($key='uinfo',$array)
	{
		//define save cookie,the cookie filed is 
		//username,password,firstvisit,uid
		Cookie::enset($key,$array);
	}
	
	/**
	 * get all cookie from user 
	 * @param unknown_type $array
	 */
	public static function getCookie($key,$encry = true)
	{
		return $encry ? Cookie::enget($key) : Cookie::get($key);
	}
	
	
	public static function updateProfile($array)
	{
		$r = false;
		$r = self::save('userinfo', $array);
		self::addToEventQueue($array,__CLASS__.':'.__METHOD__);
		return $r;
	}

	/**
	 * end Session 
	 * end session and then set cookie null
	 */
	public static function endSession($id)
	{
		//@TODO:set cookie set null
		$array['id'] = $id;
		$array['logoutTime'] = time();
		self::addToEventQueue($array,__CLASS__.':'.__METHOD__);
		//Session::
		Session::destroy();
		Cookie::setnull('autologin');
		Cookie::setnull('uinfo');
		return true;
	}
	
	
	/**
	 * forget password
	 * 
	 * @param unknown_type $array
	 */
	public static function changePassword($array)
	{
		$account = self::get('account', $array['id'],'password');
		if(isset($account['password']) && $array['password'] == $account['password'])
		{
			//set new password
			$data['id'] = $array['id'];
			$data['password'] = $array['newpassword'];
			$result = self::save('account', $data);
			if($result)
			{
				self::addToEventQueue($array,__CLASS__.':'.__METHOD__);
				return true;
			}
		}
		return false;
	}
	
	
	public static function forgetPassword($array)
	{
		$account = self::get('account', $array['id'],'email');
		if(isset($account['email']))
		{
			$array['email'] = $account['email'];
			self::addToEventQueue($array,__CLASS__.':'.__METHOD__);
			return true;
		}
		return false;
	}
	
	/**
	 * @todo:checklogin
	 * tologin
	 * @param $status  0 not in   1 success  2 password error
	 */
	public static function checkLogin($array)
	{
		//user not in the system
		$status = array();
		$account = self::get('account','','',"email='{$array['email']}'");
		if($account)
		{
			
			if($array['password'] == $account['password'])
			{
				if($account['status'] == 1)
				{
					//account is forbidden
					$status['status'] = 3;
				}else{
					//set session
					$status['status'] = 0;
					$status['uid'] = $account['id'];
				}
				
			}else{
				//user or password error
				$status['status'] = 2;
			}
		}else{
			$status['status'] = 2;
		}
		self::addToEventQueue($array,__CLASS__.':'.__METHOD__);
		return $status;
	}
	
	
	
	public static function forbidAccount($array)
	{
		$r = false;
		$array['status'] = 1;
		$r = self::save('account', $array);
		return $r;
	}
	
	
	
	public static function updatePrivacy($array)
	{
		//
	}
	
	public static function getPrivacy($array)
	{
		//
	}
	
	
	/**
	 * use user id 
	 * 
	 * @param array $array
	 */
	public static function account($array,$all=false)
	{
		$account = self::get('account', $array['id']);
		if(!$all && is_array($account)){
			return $account;
		}
		$userinfo = self::get('userinfo', $array['id']);
		if(is_array($account) && is_array($userinfo)){
			return array_merge($account,$userinfo);
		}
		return false;
	}

	/**
	 * get Account data
	 * @param string $id
	 */
	public static function accountAll($id)
	{
		return self::account(array('id'=>$id),true);
	}
	
	public static function userinfo($array)
	{
		return self::get('userinfo', $array['id']);
	}
	
	
	public static function accountList($where)
	{
		return self::gets('account', $where.' and status=0','id,account,screenname,image_id');
	}
	
	/**
	 * get new account
	 * @param unknown_type $n
	 */
	public static function newaccount($n=10)
	{
		return self::gets('account', "status =0 order by id DESC limit {$n}",'id,account,screenname,image_id');
		
	}
	/**
	 * get account in the list
	 * @param $array
	 */
	public static function accountin($array,$key='id')
	{
		$str = strin($array,$key);
		return self::gets('account', "id in $str",'id,account,screenname,image_id');
	}
	
	
	public static function saveAvatar($array)
	{
		//do save avtar
		if(!empty($array['id']) && !empty($array['image_id']) && $array['id']>0){
			
			return self::save('account', $array);
		}
		return false;
	}
	
	
}